package sz.szsmk.citizencard.mvp.presenter;

import com.google.gson.Gson;
import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.GsdxNetApi;
import sz.szsmk.citizencard.model.network.bean.request.AddAddressRequest;
import sz.szsmk.citizencard.model.network.bean.request.UpdataAdressRequest;
import sz.szsmk.citizencard.model.network.bean.response.IbaseBean;
import sz.szsmk.citizencard.mvp.contract.AdressAddContact;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.NetworkDateSources;
import sz.szsmk.citizencard.retrofitrxjava.SubscribeResult;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.Constant;

import javax.inject.Inject;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/7/11 8:58
 * description:
 */
public class AdressAddPresenter extends IBasePresenter
        implements AdressAddContact.IAdressAddPresenter<AdressAddContact.IAdressAddView> {


    private NetworkDateSources mNetworkDateSource;
    GsdxNetApi gsdxNetApi;

    @Inject
    public AdressAddPresenter(GsdxNetApi gsdxNetApi) {
        this.gsdxNetApi = gsdxNetApi;

        this.mNetworkDateSource = NetworkDateSources.getInstance();
    }


    @Override
    public void addAdress(String name, String phone, String desAddress, int isDefault, String city) {
        AddAddressRequest addressRequest = new AddAddressRequest();

        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        addressRequest.setAccess_token(access_token);
        addressRequest.setAddress(desAddress);
        addressRequest.setContact(name);
        addressRequest.setIsDefault(isDefault);
        addressRequest.setLocation(city);
        addressRequest.setMobile(phone);
        addressRequest.setPostcode("215000");
        String json = new Gson().toJson(addressRequest);
        L.e(Constant.TAG, "addAdress======" + json);
        mNetworkDateSource.addAdress(addressRequest, new SubscribeResult<IbaseBean>() {
            @Override
            protected void onOk(IbaseBean response) {
                ((AdressAddContact.IAdressAddView) mView).addSuccess();
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });

    }

    @Override
    public void updataAdress(String name, String phone, String desAddress, int isDefault, int adressId, String city) {
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        UpdataAdressRequest updataAdressRequest = new UpdataAdressRequest();
        updataAdressRequest.setAddressId(adressId);
        updataAdressRequest.setAddress(desAddress);
        updataAdressRequest.setMobile(phone);
        updataAdressRequest.setIsDefault(isDefault);
        updataAdressRequest.setContact(name);
        updataAdressRequest.setAccess_token(access_token);
        updataAdressRequest.setLocation(city);
        updataAdressRequest.setPostcode("215000");
        String json = new Gson().toJson(updataAdressRequest);
        L.e(Constant.TAG, "updataAdress  Req=========" + json);
        mNetworkDateSource.updataAdress(updataAdressRequest, new SubscribeResult<IbaseBean>() {
            @Override
            protected void onOk(IbaseBean response) {
                String json1 = new Gson().toJson(response);
                L.e(Constant.TAG, "updataAdress  Res=========" + json1);
                ((AdressAddContact.IAdressAddView) mView).updataSuccess();
            }

            @Override
            protected void onFailure(IbaseBean response) {
                super.onFailure(response);
                ((AdressAddContact.IAdressAddView) mView).updataFaile(response);
            }

            @Override
            protected void onNext(IbaseBean response) {
                super.onNext(response);
                ((AdressAddContact.IAdressAddView) mView).updataFaile(response);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }
}
